package com.benz.project.pvm.controller.version;

import com.benz.project.pvm.controller.BaseController;
import com.benz.project.pvm.http.param.version.VersionEntity;
import com.benz.project.pvm.http.vo.ResponseEntity;
import com.benz.project.pvm.service.VersionService;
import com.benz.project.pvm.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/version")
public class AddVersion extends BaseController {

    @Autowired
    private VersionService versionService;

    @RequestMapping(value="/add", method = RequestMethod.POST)
    public ResponseEntity handle(@RequestBody Map<String, Object> params) {
        ResponseEntity responseEntity = new ResponseEntity();
        Long handleStart = System.currentTimeMillis();
        try{
            handleDetail(params);
        }catch(Exception e){
            responseEntity.setError(e.getMessage());
            responseEntity.setMessage("新增版本出错");
            responseEntity.setStatus(500);
        }finally{
            Long handleEnd = System.currentTimeMillis();
            responseEntity.setUseTime(handleEnd - handleStart);
            return responseEntity;
        }
    }

    private void handleDetail(Map<String, Object> params) throws Exception{
        VersionEntity version = (VersionEntity) BeanConvertUtil.mapToObject(params, VersionEntity.class);
        versionService.addVersion(version.getName(),
                version.getVersion(),
                version.getDesc(),
                version.getProject(),
                version.getType(),
                version.getUpdateTime(),
                version.getHandler());
    }
}
